<?php
namespace ITSociaal\Themes\Lumber\Hooks;

use WP_Error;

/**
 * Main theme hooks
 */
class Main implements HooksInterface {

	/**
	 * @param object $actions
	 * @param object $filters
	 *
	 * @return void
	 */
	public function adjustHooks($actions, $filters) {
		$actions->wp->priority = -1;

		if (false
		|| !defined("APPLICATION_ENV")
		|| (constant("APPLICATION_ENV") != "development")
		) {
			unset($filters->pre_http_request);
		} else {
			$filters->pre_http_request->priority = 65535;
			$filters->pre_http_request->accepted_args = 3;
		}

		$filters->http_headers_useragent->priority = 65535;
	}

	/**
	 * Things to do before any other event
	 *
	 * @return void
	 */
	public function setup() {
		remove_action("wp_head", "_wp_render_title_tag", 1);
		remove_action("wp_head", "rel_canonical");
		remove_action("wp_head", "rest_output_link_wp_head");
		remove_action("wp_head", "rsd_link");
		remove_action("wp_head", "wlwmanifest_link");
		remove_action("wp_head", "wp_generator");
		remove_action("wp_head", "wp_oembed_add_discovery_links");
		remove_action("wp_head", "wp_shortlink_wp_head");

		// Nerf wptexturize before it breaks anything
		add_filter("run_wptexturize", "__return_false", 32767);
		wptexturize("word", true);

		remove_action("template_redirect", "rest_output_link_header", 11);
		remove_action("template_redirect", "wp_shortlink_header", 11);
		add_filter("xmlrpc_enabled", "__return_false");
		add_filter("file_mod_allowed", "__return_false");
	}

	/**
	 * body_class filter handler
	 *
	 * @param array $classes
	 *
	 * @return array
	 */
	public function filterBodyClass($classes) {
		return $this->filterIDs($classes);
	}

	/**
	 * get_avatar_data filter handler
	 *
	 * @param array $args
	 *
	 * @return array
	 */
	public function filterGetAvatarData($args) {
		if (true
		&& array_key_exists("url", $args)
		&& is_string($args["url"])
		&& preg_match(
			"/^https?:\\/\\/.*\\.gravatar\\.com\\/avatar/",
			$args["url"]
		)) {
			$args["found_avatar"] = false;
			$args["url"] = null;
		}

		return $args;
	}

	/**
	 * http_headers_useragent filter handler
	 *
	 * @param string $ua
	 *
	 * @return string
	 */
	public function filterHttpHeadersUseragent($ua) {
		// Remove URL from User-Agent string
		$ret = substr($ua, 0, strpos($ua, ";"));

		return $ret;
	}

	/**
	 * nav_menu_css_class filter handler
	 *
	 * @param array $classes
	 *
	 * @return array
	 */
	public function filterNavMenuCssClass($classes) {
		return array_diff(
			$this->filterIDs($classes),
			[
				"menu-item-type-",
				"menu-item-object-",
			]
		);
	}

	/**
	 * pre_http_request filter handler
	 *
	 * @param bool|array|\WP_Error $preempt
	 * @param array                $r
	 * @param string               $url
	 *
	 * @return bool|array
	 */
	public function filterPreHttpRequest($preempt, $r, $url) {
		$parsed = parse_url($url);

		if ($parsed["host"] == "api.wordpress.org") {
			return [
				"headers" => [],
				"body" => "",
				"response" => [
					"code" => 199,
					"message" => "LOL",
				],
				"cookies" => [],
				"filename" => "nope.txt",
			];
		}

		return false;
	}

	/**
	 * rest_authenticatoin_errors filter handler
	 *
	 * @param \WP_Error|bool|null $access
	 *
	 * @return \WP_Error|bool|null
	 */
	public function filterRestAuthenticationErrors($access) {
		if (!is_user_logged_in()) {
			return $this->createObject(
				WP_Error::class,
				"rest_cannot_access",
				"Only authenticated users can access the REST API.",
				["status" => rest_authorization_required_code()]
			);
		}

		return $access;
	}

	/**
	 * after_setup_theme action handler
	 *
	 * @return void
	 */
	public function onAfterSetupTheme() {
		add_theme_support("automatic-feed-links");
		add_theme_support("post-formats");
		add_theme_support("post-thumbnails");
		add_theme_support("menus");
		add_theme_support("html5", [
			"comment-list",
			"comment-form",
			"search-form",
			"gallery",
			"caption",
		]);
		add_theme_support("title-tag");
	}

	/**
	 * customize_register action handler
	 *
	 * @param \WP_Customize_Manager $wp_customize
	 *
	 * @return void
	 */
	public function onCustomizeRegister($wp_customize) {
		$wp_customize->remove_section("custom_css");
	}

	/**
	 * wp action handler
	 *
	 * @return void
	 */
	public function onWp() {
		header_remove("X-Pingback");
	}

	/**
	 * @param array $array
	 *
	 * @return array
	 */
	protected function filterIDs($array) {
		return array_filter(
			$array,
			function($class) {
				return !preg_match("/\w+-\d+/", $class);
			}
		);
	}

}

/* vi:set ts=4 sw=4 noet: */
